package com.ljh.onlinetest.service.impl;

import com.ljh.onlinetest.domain.ExamPaper;
import com.ljh.onlinetest.domain.ExamPaperAnswer;
import com.ljh.onlinetest.domain.TaskExamCustomerAnswer;
import com.ljh.onlinetest.domain.TextContent;
import com.ljh.onlinetest.domain.task.TaskItemAnswerObject;
import com.ljh.onlinetest.repository.BaseMapper;
import com.ljh.onlinetest.repository.TaskExamCustomerAnswerMapper;
import com.ljh.onlinetest.service.TaskExamCustomerAnswerService;
import com.ljh.onlinetest.service.TextContentService;
import com.ljh.onlinetest.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author 余一
 * @date 2022/3/6  16:36
 */
@Service
public class TaskExamCustomerAnswerServiceImpl extends BaseServiceImpl<TaskExamCustomerAnswer> implements TaskExamCustomerAnswerService {
    private final TaskExamCustomerAnswerMapper taskExamCustomerAnswerMapper;
    private final TextContentService textContentService;

    @Autowired
    public TaskExamCustomerAnswerServiceImpl(TaskExamCustomerAnswerMapper taskExamCustomerAnswerMapper, TextContentService textContentService) {
        super(taskExamCustomerAnswerMapper);
        this.taskExamCustomerAnswerMapper = taskExamCustomerAnswerMapper;
        this.textContentService = textContentService;
    }

    @Override
    public List<TaskExamCustomerAnswer> selectByTUid(List<Integer> tIds, Integer id) {
        return taskExamCustomerAnswerMapper.selectByTUid(tIds,id);
    }

    @Override
    public void insertOrUpdate(ExamPaper examPaper, ExamPaperAnswer examPaperAnswer, Date now) {
        Integer taskId = examPaper.getTaskExamId();
        Integer userId = examPaperAnswer.getCreateUser();
        TaskExamCustomerAnswer customerAnswer = taskExamCustomerAnswerMapper.getByTUid(taskId,userId);
        if (null == customerAnswer){
            customerAnswer = new TaskExamCustomerAnswer();
            customerAnswer.setCreateTime(now);
            customerAnswer.setCreateUser(userId);
            customerAnswer.setTaskExamId(taskId);
            List<TaskItemAnswerObject> taskItemAnswerObjects = Arrays.asList(new TaskItemAnswerObject(examPaperAnswer.getExamPaperId(), examPaperAnswer.getId(), examPaperAnswer.getStatus()));
            TextContent textContent = textContentService.jsonConvertInsert(taskItemAnswerObjects, now, null);
            textContentService.insertByFilter(textContent);
            customerAnswer.setTextContentId(textContent.getId());
            insertByFilter(customerAnswer);
        }else {
            TextContent textContent = textContentService.selectById(customerAnswer.getTextContentId());
            List<TaskItemAnswerObject> taskItemAnswerObjects = JsonUtil.toJsonListObject(textContent.getContent(), TaskItemAnswerObject.class);
            taskItemAnswerObjects.add(new TaskItemAnswerObject(examPaperAnswer.getExamPaperId(),examPaperAnswer.getId(),examPaperAnswer.getStatus()));
            textContentService.jsonConvertUpdate(textContent,taskItemAnswerObjects,null);
            textContentService.updateByIdFilter(textContent);
        }
    }


}
